package com.mdc.mansion.api.web.modules.order;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mdc.mansion.api.web.common.web.BaseController;
import com.mdc.mansion.common.exception.BusinessException;
import com.mdc.mansion.common.response.Result;
import com.mdc.mansion.common.response.ResultCode;
import com.mdc.mansion.common.util.DateUtils;
import com.mdc.mansion.common.util.I18nUtil;
import com.mdc.mansion.core.common.restaurant.RestaurantCommon;
import com.mdc.mansion.core.modules.order.RestaurantOrderService;
import com.mdc.mansion.dataaccess.common.context.ContextHolder;
import com.mdc.mansion.dataaccess.modules.order.entity.RestaurantOrder;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.Restaurant;
import com.mdc.mansion.dataaccess.modules.restaurant.vo.RestaurantTableVo;

@Controller
@RequestMapping(value = "${adminPath}/order")
public class RestaurantOrderController extends BaseController {

    @Resource
	private RestaurantOrderService restaurantOrderService;

	/**
	 * 修改用户订单对应的餐桌号
	 * @return
	 */
	@RequestMapping(value = "/changeTable",method  = RequestMethod.POST)
	public Result changeTable(String data)
	{
		Result restultResult = new Result(ResultCode.COMMON_SUCCESS);
		RestaurantOrder restaurantOrder = null;
		JSONObject parameterObj = null;

		restaurantOrder = new RestaurantOrder();
		parameterObj = JSONObject.parseObject(data);
		Restaurant restaurant = ContextHolder.currentRestaurant();
		restaurantOrder.setRestaurantId(restaurant.getId());
		
		if(! (parameterObj.containsKey("restaurantOrderId") || parameterObj.containsKey("restaurantTableId")))
		{
			restultResult.setCode(ResultCode.COMMON_EXCEPTION);
			return restultResult;
		}
		
		restaurantOrder.setId(parameterObj.getString("restaurantOrderId"));
		restaurantOrder.setRestaurantTableId(parameterObj.getString("restaurantTableId"));
		
		restaurantOrderService.changeTable(restaurantOrder);

		return restultResult;
	}


	//餐桌账单点餐
	@RequestMapping("submit")
	public Result submit(String data){
		Result result=new Result(ResultCode.COMMON_SUCCESS);
		restaurantOrderService.submit(data);
		return result;
	}

	//餐桌现有账单查询
	@RequestMapping("get")
	public Result get(String data){
		Result result=new Result(ResultCode.COMMON_SUCCESS);
		Map<String,Object> restaurantOrderItemDTOs=restaurantOrderService.getNotCheckOutOrderItems(data);
		result.setProperty("dishesOrderItemList",restaurantOrderItemDTOs.get("dishesOrderItemList"));
		result.setProperty("itemDetailList", restaurantOrderItemDTOs.get("itemDetailList"));
		result.setProperty("totalPrice",restaurantOrderItemDTOs.get("totalPrice"));
		result.setProperty("abatement", restaurantOrderItemDTOs.get("abatement"));
		result.setProperty("servicePrice",restaurantOrderItemDTOs.get("servicePrice"));
		result.setProperty("actualReceivePrice", restaurantOrderItemDTOs.get("actualReceivePrice"));
		result.setProperty("refundPrice",restaurantOrderItemDTOs.get("refundPrice"));
		result.setProperty("tradeNo",restaurantOrderItemDTOs.get("tradeNo"));
		result.setProperty("notifyUrl",restaurantOrderItemDTOs.get("notifyUrl"));
		return result;
	}
 
	//餐桌账单加急
	@RequestMapping("urgent")
	public Result urgent(String data){
		Result result=new Result(ResultCode.COMMON_SUCCESS);
		restaurantOrderService.urgent(data);
		return result;
	}

	//餐桌账单撤销
	@RequestMapping("cancel")
	public Result cancel(String data){
		Result result=new Result(ResultCode.COMMON_SUCCESS);
		restaurantOrderService.cancel(data);
		return result;
	}
	
	//餐桌账单撤销
	@RequestMapping("orderCancelAdmin")
	public Result orderCancelAdmin(String data){
		Result result=new Result(ResultCode.COMMON_SUCCESS);
		restaurantOrderService.orderCancel(data,RestaurantCommon.UserTypeAdmin);
		return result;
	}
	
	//餐桌账单撤销
	@RequestMapping("orderCancelFwy")
	public Result orderCancelFwy(String data){
		Result result=new Result(ResultCode.COMMON_SUCCESS);
		restaurantOrderService.orderCancel(data,RestaurantCommon.UserTypeFwy);
		return result;
	}

	//餐桌添加账单,该接口暂时不需要
	@RequestMapping("submitNewBill")
	public Result submitNewBill(String data){
		Result result=new Result(ResultCode.COMMON_SUCCESS);
		return result;
	}

	//餐桌添加账单,该接口暂时不需要
	@RequestMapping("add")
	public Result add(String data){
		Result result=new Result(ResultCode.COMMON_SUCCESS);
		restaurantOrderService.orderAddItem(data);
		return result;
	}
	
    @RequestMapping("orderList")
    public Result getOrderList(String data)
    {
    	Result restultResult=new Result(ResultCode.COMMON_SUCCESS);
    	Restaurant restaurant = ContextHolder.currentRestaurant();
    	String pageSize = null;
    	String pageNo = null;
    	List<RestaurantTableVo> orderList = null;
    	 JSONArray jsonArray = new JSONArray();
    	try
    	{
    	    JSONObject jsonParam = JSONObject.parseObject(data);
    		pageSize = jsonParam.getString("pageSize");
    		pageNo = jsonParam.getString("pageNo") ;
    		orderList = restaurantOrderService.getOrderStatusList(restaurant,pageSize,pageNo);
    		if (orderList != null && orderList.size() > 0) {
                for (RestaurantTableVo restaurantTableVo : orderList) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("tableNum", restaurantTableVo.getNum());
                    jsonObject.put("id", restaurantTableVo.getId());
                    jsonObject.put("name", restaurantTableVo.getName());
                    jsonObject.put("englishName", restaurantTableVo.getEnglishName());
                    jsonObject.put("chineseName", restaurantTableVo.getChineseName());
                    jsonObject.put("orderStatus", restaurantTableVo.getOrderStatus());
                    jsonObject.put("orderId", restaurantTableVo.getOrderId());
                    jsonObject.put("updateTime", DateUtils.format(restaurantTableVo.getCreateTime(),"HH:mm"));
                    jsonArray.add(jsonObject);
                }
            }
    		
    		restultResult.setProperty("orderList", jsonArray);
    		
    	}catch(Exception e){
    	    e.printStackTrace();
			/**获取订单列表失败**/
			throw new BusinessException(I18nUtil.getMessage("table.staus.list.fail.exception"));
		}
    	return restultResult;
    }

	@RequestMapping("affirm")
	public Result affirm(String data){
		Result result=new Result(ResultCode.COMMON_SUCCESS);
//		ContextHolder.currentRestaurant().setId("90f106cb4612450d9fdd58f43a44ce12");
		restaurantOrderService.affirm(data);
		return result;
	}
}
